<?php
$server= "10.0.0.80:3306"; /* Adresse des 1&amp;1 Datenbankservers */
$user= "wolfgang"; /* Datenbank-Benutzername */
$passwort= "wolfgang"; /* Passwort */
$datenbank= "racecontrol"; /* Name der Datenbank */
$rang; /* Rang des Users */
$time = mktime(20, 52, 0, 9, 15, 2014); //Stunde, Minute, Sekunde, Monat, Tag, Jahr;
$timenow = time();
/* Zugriff auf SQL-Server und Abfragen der Tabelle */
MYSQL_CONNECT($server, $user, $passwort) or die ("<H3>Datenbankserver nicht erreichbar</H3>");
MYSQL_SELECT_DB($datenbank) or die ( "<H3>Datenbank nicht vorhanden</H3>");

$result=MYSQL_QUERY("select count(lap.time) as Runden,
 time_to_sec(max(lap.time)) - time_to_sec(min(lap.time)) as Zeit,
 starter.startNumber as Startnummer,
 starter.firstName as Vorname , starter.lastName as Nachname
 from lap, starter ,race
where lap.raceId = race.raceId
  and race.active = '1'
  and lap.tagId = starter.tagId
  and lap.raceId = starter.raceId
 group by lap.tagId order by count(lap.time) desc ,time_to_sec(max(lap.time)) - time_to_sec(min(lap.time)) asc");
function zeitformat($Sekundenzahl)
{
  $Sekundenzahl = abs($Sekundenzahl); // Ganzzahlwert bilden

  return sprintf("%02d:%02d:%02d",
                ($Sekundenzahl/60/60)%24,($Sekundenzahl/60)%60,$Sekundenzahl%60);
}
function printHeader($result)
{
  echo "<table border=\"1\" align=center width=100% >";
  echo "<tr  style=\"color:#FFFF00\" >";
  echo "<th>Rang</A></th>";
  while ($field=mysql_fetch_field($result)) {
    echo "<th>$field->name</A></th>";
  }
  echo "</tr>";
}
/* Ausgabe des countowns */
$countown = $time - $timenow;
$countownPrint = zeitformat($countown);
if($time < $timenow){
?  echo "<h2 align=center > Verbleibende Zeit: 00:00:00 </h2>";
}else{
echo "<h2 align=center > Verbleibende Zeit: $countownPrint </h2>";
}
/* Ausgabe der Tabelle in einer HTML-Table */
echo "<span style=\"color:#FFFF00\" font-weight:bold>";
echo "<table border=\"1\" align=center width=100% >";
echo "<tr  style=\"color:#FFFF00\" >";
echo "<td align=center style=\"color:#FFFF00\" font-weight:bold>";
printHeader($result);
$rang = 1;
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td align=center style=\"color:#FFFF00\" font-weight:bold> $rang </td>";
$rang++;
for($i=0; $i < mysql_num_fields($result); $i++) {
  if ($i == 1) {
    $zeit = zeitformat($row[$i]);
    echo "<td align=center style=\"color:#FFFF00\" font-weight:bold> $zeit </td>";
  }elseif( $i == 0){
    $runden = $row[$i] -1 ;
    echo "<td align=center style=\"color:#FFFF00\" font-weight:bold>  $runden </td>";
  }else{
    echo "<td align=center style=\"color:#FFFF00\" font-weight:bold> $row[$i]</td>";
  }
}
echo "</tr>\n";
if($rang == 30){
  echo "</td>";
  echo "<td>";
  printHeader($result);
}
}
echo "</td>";
echo "</table>";
echo "</span>";
MYSQL_CLOSE();
?>
